/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class LoadNameData {

    static Logger logger = Logger.getLogger(LoadNameData.class);
//    public static HashMap<String, String> nameMap;//映射表
    private String configname = "";

    public LoadNameData(String configname) {
        this.configname = configname;
    }

    public HashMap<String, String> init() {
        HashMap<String, String> nameMap = new HashMap<String, String>();
        String line = "";
        String[] dvd;
        String mataFilePath = "";
        try {
            //配置文件
            mataFilePath = ClassLoader.getSystemClassLoader().getResource(configname).getFile();
            logger.info("the config mapdata is " + mataFilePath);
            BufferedReader br = new BufferedReader(new FileReader(mataFilePath));
            try {
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.isEmpty()) {
                    } else {
                        dvd = line.split("\t");
                        if (dvd.length == 2) {
                            nameMap.put(dvd[0], dvd[1]);
                        }
                    }
                }
            } catch (Exception ex) {
                nameMap = null;
                logger.error("cannot read mapData file : " + mataFilePath + ". Cause " + ex.getMessage(), ex);
            } finally {
                try {
                    br.close();
                } catch (IOException ex) {
                }
            }
        } catch (Exception ex) {
            nameMap = null;
            logger.error("cannot find mapData file : " + mataFilePath + ".mapdata. Cause " + ex.getMessage(), ex);
        }
        logger.info("load mataFilePath finish.");
        return nameMap;
    }
}
